package com.sphd.restaurantsystem.model;


import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.base.RestaurantsApplication;
import com.sphd.restaurantsystem.interfaces.VolleyStringCallBack;
import com.sphd.restaurantsystem.presenter.RestaurantPresenterImpl;
import com.sphd.restaurantsystem.utils.upload.MyMultipartRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Hu on 2017/01/13
*/

public class RestaurantModelImpl implements RestaurantModel {
    RestaurantPresenterImpl restaurantPresenter;
    @Override
    public void sendUp(String url, String image, List<File> fileList, Map<String, String> map, RestaurantPresenterImpl restaurantPresenter) {
        this.restaurantPresenter=restaurantPresenter;
        Map<String, String> newMap =new HashMap<>();
        newMap.put("cginfo",new JSONObject(map).toString());
        MyMultipartRequest multipartRequest = new MyMultipartRequest(url,
                new Response.Listener() {
                    @Override
                    public void onResponse(Object response) {
                        getRes(response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                restaurantPresenter.sendError(error);
            }
        },image, fileList, newMap);

        RestaurantsApplication.getRequestQueue().add(multipartRequest);
    }

    @Override
    public void sendUpNoImage(String url, Map<String, String> map, RestaurantPresenterImpl restaurantPresenter) {
        this.restaurantPresenter=restaurantPresenter;

        Map<String, String> newMap =new HashMap<>();
        newMap.put("cginfo",new JSONObject(map).toString());
        volleyUtil.setStringRequest(url, new VolleyStringCallBack() {
            @Override
            public void onResponse(String response) {
                getRes(response);
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                restaurantPresenter.sendError(error);
            }
        }, newMap);
    }
    private void getRes(String response) {
        Log.e("RE==",response.toString());
        try {
            restaurantPresenter.sendRes(new JSONObject(response).getInt("res"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}